package com.example.controller;

import com.example.entity.Menu;
import com.example.entity.StudentAnswer;
import com.example.entity.Subject;
import com.example.service.CourseService;
import com.example.service.MenuService;
import com.example.service.StudentAnswerService;
import com.example.utils.RestResponses;
import com.example.utils.VideoUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * @author： wzz
 * @date： 2021-10-27 15:29
 */

@Slf4j
@Controller
@RequestMapping(value = "/menu/")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private StudentAnswerService studentAnswerService;

    @Autowired
    private CourseService courseService;

    /**
     * 查询所有跳转页面
     * @return
     */
    @RequestMapping(value = "queryAll")
    @ResponseBody
    public RestResponses queryAll(){
        List<Menu> menuList = menuService.queryAll();
        if (menuList != null){
            return  RestResponses.ok(menuList);
        }else {
            return RestResponses.fail();
        }

    }


    /**
     * 跳转到content页面
     * @return
     */
    @RequestMapping(value = "toAddInfo")
    @ResponseBody
    public RestResponses toAddInfo(@RequestParam(value = "id") String id,Model model){


        model.addAttribute("id",id);

        return RestResponses.ok("yes",200);

    }

    /**
     * 最终跳转到content页面
     * @return
     */
    @RequestMapping(value = "AddInfo")
    public String AddInfo(@RequestParam(value = "id") String id,Model model){

        model.addAttribute("id",id);

        return "/back/study/content";

    }


    /**
     * 跳转到前端的学习页面
     * @return
     */
    @RequestMapping(value = "front/toStudyPage")
    public String  toStudyPage(@RequestParam(value = "book_name") String book_name,
                               @RequestParam(value = "menu_topic") String menu_topic,
                               Model model, Authentication authentication) throws IOException {


        if(menuService.queryByChapterNo(menu_topic,book_name).size()==0){
            model.addAttribute("message","等待教师上传内容");
            return "front/study/content";
        }
        model.addAttribute("menu_topic",menu_topic);
        model.addAttribute("book_name",book_name);

        //选择题
        List<Subject> list = menuService.queryByChapterNo(menu_topic,book_name);

        model.addAttribute("list",list);

        //获取video的地址
        String video = courseService.videoUrl(book_name,menu_topic);
        if ( null != video){
            model.addAttribute("videoUrl",video);
            long duration = VideoUtil.getDuration(System.getProperty("user.dir")+"/static"+video);
            int s = courseService.videoDuration(book_name,menu_topic, duration);
            log.info("视频的总时长情况："+s);
        }
        else
            model.addAttribute("videoUrl","#");

        //将文件的地址添加到列表中
        String ppt = courseService.fileUrl(book_name,menu_topic);
        if (null != ppt){
            model.addAttribute("fileUrl",ppt);
        }else {
            model.addAttribute("fileUrl","本章尚未上传文件");
        }

        String studentNumber = authentication.getName();
        //根据学号获取该学生答得题目
       Boolean alreadyOption = false;
       Boolean alreadyMut = false;
       Boolean alreadyFil = false;
       Boolean alreadyCode = false;
        List<StudentAnswer> answerList = studentAnswerService.getAnswers(studentNumber,book_name,menu_topic);
        for (StudentAnswer answer :answerList){
            switch (answer.getType()){
                case "1":alreadyOption=true;break;
                case "2":alreadyMut=true;break;
                case "3":alreadyFil=true;break;
                case "4":alreadyCode=true;break;
            }
        }
        model.addAttribute("alreadyOption",!alreadyOption);
        model.addAttribute("alreadyMut",!alreadyMut);
        model.addAttribute("alreadyFil",!alreadyFil);
        model.addAttribute("alreadyCode",!alreadyCode);
        //将视频的观看时长添加到表中
        int lookTime = 0 ;
        if (answerList.size() != 0)// if (answerList != null)
            lookTime = answerList.get(0).getVideo();

        model.addAttribute("lookTime",lookTime);


        for (int i=0; i<answerList.size(); i++){
            for (int j=0; j<list.size(); j++){
                if ( answerList.get(i).getQuestionNo().equals(list.get(j).getQuestion_number()) ){
                    list.get(j).setStudentAnswer(answerList.get(i).getStudentAnswer());
                    list.get(j).setScore(answerList.get(i).getScore());
                    //比对答案，学生的选项和正确的选项
                    if (list.get(j).getScore()!=null && list.get(j).getScore()!=0)
                        list.get(j).setCheck("√");
                    else
                        list.get(j).setCheck("×");
                }
            }
        }
        return "front/study/content";
    }



}
